/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';
console.log('Load O2O: ' + module.id);
let F = require('./function');
let Dao = require('./dao');
let orderDispatcher = new Dao.order();
let orderPayWayDispatcher = new Dao.orderPayWay()
let orderTicketDispatcher = new Dao.orderTicket()

function* api() {
}
exports.api = api;

/**
 * @todo 新建一个订单
 * @param Env
 */
api.add = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;


  let ret = yield orderDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);

};

/**
 * @todo 更新订单
 * @param Env
 */
api.update = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;

  let ret = yield orderDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 *@todo 获取用户订单列表
 */
api.list = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  let act = fields.act || 1;
  if (act == 1) {
    if (!fields.uid) return F.returnMsg(400, '必须有uid', 3);
  } else {
    if (!fields.vid) return F.returnMsg(400, '必须有vid', 3);
  }
  let ret = yield orderDispatcher.list(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, 'OK', 3, ret.data);
};

/**
 *@todo 获取用户单个订单
 */
api.detail = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  let ret = yield orderDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, 'OK', 3, ret.data);
};

/**
 *@todo 添加订单的支付方式
 *@param  {Object}  Env  数据
 *@ret    {Object}
 */
api.addPayWay = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;

  if (!fields.order_id) return F.returnMsg(400, '必须有订单id');
  if (!fields.pay_way) return F.returnMsg(400, '必须有订单的支付方式');


  let ret = yield orderPayWayDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);

};

api.updatePayWay = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;

  if (!fields.opy_id) return F.returnMsg(400, '必须有opy_id');
  let ret = yield orderPayWayDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 *@todo 添加瓷肌券
 *@param  {Object}  Env  数据
 *@ret    {Object}
 */
api.addTicket = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  if (!fields.order_id) return F.returnMsg(400, '必须有订单id');
  let retOrder = yield orderDispatcher.findOneForDis({orderid: fields.order_id})
  if (retOrder == 1 || !retOrder.data) return F.returnMsg(400, retOrder.msg);

  let ret = yield orderTicketDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 *@todo 更新瓷肌券
 * { "id":xxx,
 *   "update":{foo:bar}
 * }
 *@param  {Object}  Env  数据
 *@ret    {Object}
 */
api.updateTicket = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  //if (!fields.order_id) return F.returnMsg(400, '必须有订单id');
  //let retOrder = yield orderDispatcher.findOneForDis({orderid: fields.order_id})
  //if (retOrder == 1 || !retOrder.data) return F.returnMsg(400, retOrder.msg);

  let ret = yield orderTicketDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};


/**
 * post /order/getTicket
 * { orderid:1 }
 * { orderid:[1,3] }
 * @param Env
 */
api.getTicket = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  let ret = yield orderTicketDispatcher.getTicket(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * 获取瓷肌券详情
 * {ticket_code:1}
 * @param Env
 */
api.ticketDetail = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  let ret = yield orderTicketDispatcher.ticketDetail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};
/**
 @todo raw query
 * @param Env
 */
api.rawquery = function*(Env) {

  let result = yield orderDispatcher.rawQuery('select * from mh_order ');
  return result;
};